/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Riset1;

import Jama.*;
import Jama.EigenvalueDecomposition;
import Jama.SingularValueDecomposition;

/**
 *
 * @author Presario
 */
public class SVD {

public static void HitungSVD(double aArray[][], int m, int n) {
        // TODO code application logic here
        int M=10; int N=4;
        Matrix A = Matrix.random(M, N);
        double Aarray[][]=A.getArray();
        Aarray[0][0]=2; Aarray[0][1]=1; Aarray[0][2]=0; Aarray[0][3]=0;
        Aarray[1][0]=1; Aarray[1][1]=0; Aarray[1][2]=0; Aarray[1][3]=1;
        Aarray[2][0]=2; Aarray[2][1]=0; Aarray[2][2]=0; Aarray[2][3]=0;
        Aarray[3][0]=0; Aarray[3][1]=1; Aarray[3][2]=0; Aarray[3][3]=1;
        Aarray[4][0]=0; Aarray[4][1]=2; Aarray[4][2]=0; Aarray[4][3]=0;
        Aarray[5][0]=0; Aarray[5][1]=0; Aarray[5][2]=1; Aarray[5][3]=1;
        Aarray[6][0]=0; Aarray[6][1]=0; Aarray[6][2]=2; Aarray[6][3]=0;
        Aarray[7][0]=0; Aarray[7][1]=0; Aarray[7][2]=2; Aarray[7][3]=0;
        Aarray[8][0]=0; Aarray[8][1]=0; Aarray[8][2]=2; Aarray[8][3]=0;
        Aarray[9][0]=0; Aarray[9][1]=0; Aarray[9][2]=0; Aarray[9][3]=2;
        A=Matrix.constructWithCopy(Aarray);
        System.out.println("Matrix Awal Freq:");
        A.print(9, 6);

        SingularValueDecomposition svd = A.svd();
        Matrix u = svd.getU();
        Matrix s = svd.getS();
        Matrix v = svd.getV();
        Matrix vT = v.transpose();
        System.out.println("Matrix U:");
        u.print(9, 6);
        System.out.println("Matrix S:");
        s.print(9, 6);
        double tempS[][]=s.getArray();
        double sReducedArray[][]=new double[s.getRowDimension()-1][s.getColumnDimension()-1];
        for(int i=0; i<s.getRowDimension()-1; i++) {
            for(int j=0; j<s.getColumnDimension()-1; j++) {
                sReducedArray[i][j]=tempS[i][j];
            }
        }
        System.out.println("Matrix s reduced:");
        for(int i=0; i<s.getRowDimension()-1; i++) {
        System.out.print("\n");
            for(int j=0; j<s.getColumnDimension()-1; j++) {
                System.out.print("\t"+sReducedArray[i][j]);
            }
        }
        System.out.println("Matrix V:");
        v.print(9, 6);
        System.out.println("Matrix vT (v transpose):");
        vT.print(9, 6);
        Matrix sReduced=Matrix.constructWithCopy(sReducedArray);

        double tempT[][]=new double[vT.getRowDimension()][vT.getColumnDimension()];
        tempT=vT.getArray();
        double vTReducedArray[][]=new double[vT.getRowDimension()-1][vT.getColumnDimension()];
        for(int i=0; i<vT.getRowDimension()-1; i++) {
            for(int j=0; j<vT.getColumnDimension(); j++) {
                vTReducedArray[i][j]=tempT[i][j];
            }

        }
        System.out.println("Matrix vT reduced:");
        for(int i=0; i<vT.getRowDimension()-1; i++) {
            System.out.print("\n");
            for(int j=0; j<vT.getColumnDimension(); j++) {
                System.out.print("\t"+vTReducedArray[i][j]);
            }
        }
        Matrix vTReduced=Matrix.constructWithCopy(vTReducedArray);
        Matrix vReduced=vTReduced.transpose();
        System.out.println("vReduced[row]="+vReduced.getRowDimension()+"vReduced[col]="+vReduced.getColumnDimension());
        System.out.println("sReduced[row]="+sReduced.getRowDimension()+"sReduced[col]="+sReduced.getColumnDimension());
        Matrix vTxsReduced = vReduced.times(sReduced);
        System.out.println("Matrix vReduced x sReduced:");
        vTxsReduced.print(9, 6);
        double similar[][] = new double[vTxsReduced.getRowDimension()][vTxsReduced.getRowDimension()];
        double arrayvs[][]=vTxsReduced.getArray();
        for(int i=0; i<vTxsReduced.getRowDimension()-1; i++) {
            double tempA=0, tempB1=0, tempB2=0, tempB=0, temp=0;
            for(int ii=i+1; ii<vTxsReduced.getRowDimension(); ii++) {
                for(int j=0; j<vTxsReduced.getColumnDimension(); j++) {
                    tempA=tempA+arrayvs[i][j]*arrayvs[ii][j];
                    tempB1=tempB1+arrayvs[i][j]*arrayvs[i][j];
                    tempB2=tempB2+arrayvs[ii][j]*arrayvs[ii][j];
                }
                tempB1=Math.sqrt(tempB1);
                tempB2=Math.sqrt(tempB2);
                tempB=tempB1*tempB2;
                temp=tempA/tempB;
                similar[i][ii]=temp;
            }
        }
        System.out.println("Similarity");
        System.out.print("\n     ");
        for(int i=0; i<vTxsReduced.getRowDimension(); i++) {
            System.out.print("\tDoc-"+String.valueOf(i+1));
        }
        for(int i=0; i<vTxsReduced.getRowDimension(); i++) {
            System.out.print("\nDoc-"+String.valueOf(i+1));
            for(int j=0; j<vTxsReduced.getRowDimension(); j++) {
                if(i==j)
                    similar[i][j]=1;
                else
                if(i>j)
                    similar[i][j]=similar[j][i];
                System.out.print("\t"+similar[i][j]);
            }
        }
    }

}
